package com.swpu.project.model.page;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Map;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class PageQuery<T> {

private Integer pageNum=1;

private Integer pageSize=100;

private Integer pageStart;

private Integer pageEnd;

private Map<String,String> queryBy;

private Map<String,Boolean> sortBy;

public  void initSqlLimit()
{
 pageStart=(pageNum-1)*pageSize;
 pageEnd=pageSize;

}


public Boolean initSortBy(Page page)
{
  if(sortBy==null)
    return false;
    for (Map.Entry<String, Boolean> entry : sortBy.entrySet()) {
        String key = entry.getKey();
        Boolean value = entry.getValue();
        page.addOrder(new OrderItem(key,value));

    }

return true;
}
  public Page setPage(PageQuery pageQuery) {
        Integer pageNum = pageQuery.getPageNum();
        Integer pageSize = pageQuery.getPageSize();
        Page<T> page = Page.of(pageNum, pageSize);
        pageQuery.initSortBy(page);



        return page;

    }
}
